<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2020/9/11
  Time: 15:05
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <meta charset="utf-8">
    <title>首页--汽车租赁系统</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <link rel="stylesheet" href="${pageContext.request.contextPath}/static/layui/css/layui.css" media="all" />
    <link rel="stylesheet" href="${pageContext.request.contextPath}/static/css/public.css" media="all" />
    <link rel="stylesheet" href="${pageContext.request.contextPath}/static/layui_ext/dtree/dtree.css">
    <link rel="stylesheet" href="${pageContext.request.contextPath}/static/layui_ext/dtree/font/dtreefont.css">
</head>
<body class="childrenBody">
<!-- 搜索条件开始 -->
<fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
    <legend>查询条件</legend>
</fieldset>
<form class="layui-form" method="post" id="searchFrm">

    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">车牌号:</label>
            <div class="layui-input-inline" style="padding: 5px">
                <input type="text" name="carnumber" autocomplete="off" class="layui-input layui-input-inline"
                       placeholder="请输入车牌号" style="height: 30px;border-radius: 10px">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">车辆类型:</label>
            <div class="layui-input-inline" style="padding: 5px">
                <input type="text" name="cartype" autocomplete="off" class="layui-input layui-input-inline"
                       placeholder="请输入车辆类型" style="height: 30px;border-radius: 10px">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">车辆颜色:</label>
            <div class="layui-input-inline" style="padding: 5px">
                <input type="text" name="color" autocomplete="off" class="layui-input layui-input-inline"
                       placeholder="请输入车辆颜色" style="height: 30px;border-radius: 10px">
            </div>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">车辆描述:</label>
            <div class="layui-input-inline" style="padding: 5px">
                <input type="text" name="description" autocomplete="off" class="layui-input layui-input-inline"
                       placeholder="请输入车辆描述" style="height: 30px;border-radius: 10px">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">是否出租:</label>
            <div class="layui-input-inline">
                <input type="radio" name="isrenting" value="1" title="已出租">
                <input type="radio" name="isrenting" value="0" title="未出租">
            </div>
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-input-black" style="text-align: center">
            <button type="button"
                    class="layui-btn layui-btn-normal layui-icon layui-icon-search layui-btn-radius layui-btn-sm"
                    lay-submit  lay-filter="doSearch" style="margin-top: 4px">查询
            </button>
            <button type="reset"
                    class="layui-btn layui-btn-warm layui-icon layui-icon-refresh layui-btn-radius layui-btn-sm"
                    style="margin-top: 4px">重置
            </button>
        </div>
    </div>

</form>
<!-- 搜索条件结束 -->
<table id="carTable" lay-filter="carTable"></table>

<script id="toolbarCar" type="text/html">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm layui-btn-danger" lay-event="carAdd">添加</button>
        <button class="layui-btn layui-btn-sm layui-btn-normal" lay-event="delBatch">批量删除</button>
    </div>
</script>

<script id="barCar" type="text/html">
    <a class="layui-btn layui-btn-xs layui-btn-radius" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs layui-btn-radius" lay-event="del">删除</a>
    <a class="layui-btn layui-btn-normal layui-btn-xs layui-btn-radius" lay-event="showImg">查看大图</a>
</script>




<!-- 添加和修改的弹出层-->
<div style="display: none;padding: 20px" id="saveOrUpdateDiv">
    <form class="layui-form layui-row layui-col-space10" lay-filter="dataFrm" id="dataFrm">
        <div class="layui-col-md12 layui-col-xs12">
            <div class="layui-row layui-col-space10">
                <div class="layui-col-md9 layui-col-xs7">

                    <div class="layui-form-item magt3">
                        <label class="layui-form-label">车牌号:</label>
                        <div class="layui-input-block" style="padding: 5px">
                            <input type="text" name="carnumber" id="carnumber" autocomplete="off" class="layui-input"
                                   lay-verify="required"
                                   placeholder="请输入车牌号" style="height: 30px;border-radius: 10px">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">车辆类型:</label>
                        <div class="layui-input-block" style="padding: 5px">
                            <input type="text" name="cartype" autocomplete="off" class="layui-input"
                                   placeholder="请输入车辆类型" style="height: 30px;border-radius: 10px">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">车辆颜色:</label>
                        <div class="layui-input-block" style="padding: 5px">
                            <input type="text" name="color" autocomplete="off" class="layui-input"
                                   placeholder="请输入车辆颜色" style="height: 30px;border-radius: 10px">
                        </div>
                    </div>
                </div>
                <div class="layui-col-md3 layui-col-xs5">
                    <div class="layui-upload">
                        <button type="button" class="layui-btn" id="test1">上传图片</button>
                        <div class="layui-upload-list">
                            <img class="layui-upload-img" id="demo1" src="" style="width: 170px;height: 100px;">
                            <p id="demoText"></p>
                            <input type="hidden" name="carimg" id="carimg">
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item magb0">
                <label class="layui-form-label">车辆描述:</label>
                <div class="layui-input-block" style="padding: 5px">
                    <input type="text" name="description" autocomplete="off" class="layui-input"
                           placeholder="请输入车辆描述" style="height: 30px;border-radius: 10px">
                </div>
            </div>
            <div class="layui-form-item magb0">
                <div class="layui-inline">
                    <label class="layui-form-label">车辆价格:</label>
                    <div class="layui-input-block" style="padding: 5px">
                        <input type="text" name="price" class="layui-input" lay-verify="required|number"
                               placeholder="请输入车辆价格" style="height: 30px;border-radius: 10px">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">出租价格:</label>
                    <div class="layui-input-block" style="padding: 5px">
                        <input type="text" name="rentprice" class="layui-input" lay-verify="required|number"
                               placeholder="请输入车辆出租价格" style="height: 30px;border-radius: 10px">
                    </div>
                </div>
            </div>
            <div class="layui-form-item magb0">
                <div class="layui-inline">
                    <label class="layui-form-label">出租押金:</label>
                    <div class="layui-input-block" style="padding: 5px">
                        <input type="text" name="deposit" class="layui-input" lay-verify="required|number"
                               placeholder="请输入车辆出租押金" style="height: 30px;border-radius: 10px">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">是否出租:</label>
                    <div class="layui-input-inline">
                        <input type="radio" name="isrenting" value="1" title="已出租">
                        <input type="radio" name="isrenting" value="0" checked="checked" title="未出租">
                    </div>
                </div>
            </div>
            <div class="layui-form-item magb0">
                <div class="layui-input-block" style="text-align: center;padding-right: 120px">
                    <button
                            class="layui-btn layui-btn-normal layui-btn-md layui-icon layui-icon-release layui-btn-radius"
                            lay-filter="doSubmit" lay-submit="">提交
                    </button>
                    <button type="reset"
                            class="layui-btn layui-btn-warm layui-btn-md layui-icon layui-icon-refresh layui-btn-radius">
                        重置
                    </button>
                </div>
            </div>
        </div>
    </form>
</div>

<%--查看大图弹出的层开始--%>
<div id="viewCarImageDiv" style="display: none;text-align: center">
    <img alt="车辆图片" width="700px" height="310px" id="view_carimg">
</div>
<%-- 添加或者修改弹出框结束 --%>

<script type="text/javascript" src="${pageContext.request.contextPath}/static/layui/layui.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/static/js/index.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/static/js/cache.js"></script>
<script>
    var tab;
    layui.use(
        ['table', 'layer', 'form', 'jquery','upload'], function () {
            var table = layui.table;
            var form = layui.form;
            var $ = layui.jquery;
            var layer = layui.layer;
            var upload=layui.upload;
            // var dtree = layui.dtree;


            tab = table.render({
                elem: '#carTable',
                url: "${pageContext.request.contextPath}/rent/carShow",
                cellMinWidth: 30,
                page: {limit: 5, limits: [5, 10, 15, 20]},
                toolbar: '#toolbarCar',//开启头部工具栏，并为其绑定左侧模板
                cols: [[
                    {type: 'checkbox', fixed: 'left'},
                    {field: 'carnumber', width: 150, title: '车牌号', sort: true}
                    ,{field: 'cartype', width: 80, title: '车辆类型', sort: true}
                    , {field: 'color', width: 80, title: '车辆颜色', sort: true}
                    , {field: 'price', width: 80, title: '车辆价格'}
                    ,{field: 'rentprice', width: 110, title: '出租价格', sort: true}
                    ,{field: 'deposit', width: 80, title: '押金', sort: true}
                    ,{field: 'isrenting', width: 80, title: '是否出租', sort: true,templet: function (data) {
                            return data.isrenting == 1 ? '已出租' : '未出租'
                        }}
                    ,{field: 'description', width: 130, title: '描述', sort: true}
                    ,{field: 'createtime', width: 120, title: '创建时间', sort: true}
                    , {fixed: 'right', title: '操作', toolbar: '#barCar'}
                ]]
            })
            var index;
            //头工具栏事件
            table.on('toolbar(carTable)', function (obj) {
                if (obj.event == 'carAdd') {
                    carAdd()
                } else if (obj.event == 'delBatch') {
                    delBatch()
                }
            })
            //普通图片上传
            var uploadInst=upload.render({
                elem:'#test1',
                url:'${pageContext.request.contextPath}/rent/uploadImg',
                acceptMime:'image/*',
                field:'f1',
                done:function(res){
                    //上传失败
                    if(res.code>0){
                        return layer.msg("上传失败")
                    }
                    //上传成功
                    $("#demo1").attr("src","/car"+res.data.src)
                    $("#carimg").val("/car"+res.data.src)
                }
            })
            var url;
            function carAdd() {
                index = layer.open({
                    type: 1,
                    title: '添加车辆',
                    content: $("#saveOrUpdateDiv"),
                    area: ['800px', '440px'],
                    success: function () {
                        // 页面数据要清除
                        $("#dataFrm")[0].reset();
                        url = "${pageContext.request.contextPath}/rent/addCar"
                    }
                })
            }
            function delBatch(){
                var data1 = table.checkStatus('carTable').data
                // var data1=checkStatus.data;
                var ids="";
                var count=0;
                for(var i=0;i<data1.length;i++){
                    ids+=data1[i].carnumber+",";
                    count++;
                }

                if(count == 0){
                    return;
                }
                ids = ids.substring(0,ids.length-1)

                layer.confirm("确定删除这"+count+"条数据吗",function(r){
                    layui.$.get("${pageContext.request.contextPath}/rent/deleteAllCar?ids="+ids,function(rr){
                        layer.msg(rr.msg)
                        tab.reload()
                    })
                })
            }

            table.on('tool(carTable)', function (obj) {
                var data = obj.data
                if (obj.event == "edit") { // 编辑
                    carUpdate(data)
                } else if (obj.event == "del") { // 删除
                    carDelete(data.carnumber)
                }else if(obj.event=="showImg"){
                    //查看大图
                    showImg(data)
                }
            })
            form.on('submit(doSubmit)', function (data) {
                var param = $("#dataFrm").serialize()
                layer.confirm("确定要提交该数据么", function (r) {
                    $.get(url+"?" + param, function (rr) {
                        layer.msg(rr.msg)
                        layer.close(index)
                        tab.reload()
                    })
                })
                return false;
            })
            form.on('submit(doSearch)', function (data) {
                //ajax数据
                tab.reload({
                    where: data.field
                })
                //不跳转页面
                return false;
            })

            function carUpdate(data) {
                index = layer.open({
                    type: 1,
                    title: '修改车辆',
                    content: $("#saveOrUpdateDiv"),
                    area: ['800px', '440px'],
                    success: function () {
                        // 给form表单设置值
                        // 把当前行的数据data显示到form表单中去,form表单中是有id隐藏字段
                        form.val("dataFrm", data)
                        $("#demo1").attr("src",data.carimg)

                        // 下拉框设置值
                        // if (data.userid != 0) {
                        //     //把userid保存到隐藏字段
                        //     $("#userid").val(data.userid)
                        // }
                        // 给pid隐藏字段赋值
                        url = "${pageContext.request.contextPath}/rent/updateCar"
                    }
                })
            }
            function showImg(data){
                index=layer.open({
                    type: 1,
                    title:'查看大图',
                    content:$("#viewCarImageDiv"),
                    area: ['800px', '440px'],
                    success:function(){
                        $("#view_carimg").attr("src",data.carimg)
                    }
                })
            }

            function carDelete(carnumber) {
                layer.confirm("确定要删除该数据吗？", function () {
                    $.post("${pageContext.request.contextPath}/rent/carDelete", {"carnumber": carnumber}, function (r) {
                        layer.msg(r.msg)
                        tab.reload()
                    })
                })
            }

            <%--var roleTable--%>

            <%--function selUserRole(data) {--%>
                <%--// 弹出框--%>
                <%--index = layer.open({--%>
                    <%--type: 1 // 1（页面层）--%>
                    <%--, title: "分配" + data.realname + "的角色"--%>
                    <%--, content: $("#selUserRoleDiv")--%>
                    <%--, area: ['600px', '380px']--%>
                    <%--, btn: ['<div class="layui-icon layui-icon-release">确认分配</div>'--%>
                        <%--, '<div class="layui-icon layui-icon-close">取消分配</div>']--%>
                    <%--, btnAlign: 'c'--%>
                    <%--, success: function () {--%>
                        <%--// 给roleTable赋值--%>
                        <%--roleTable = table.render({--%>
                            <%--elem: "#roleTable"--%>
                            <%--, url: "${pageContext.request.contextPath}/user/findRoleByUser?userid=" + data.userid--%>
                            <%--// ,page: {limit:5 ,limits:[5,10,15]} //开启分页--%>
                            <%--, cellMinWidth: 80--%>
                            <%--, cols: [[ //表头--%>
                                <%--{type: 'checkbox', fixed: 'left'}--%>
                                <%--, {field: 'roleid', title: '角色ID', fixed: 'center'}--%>
                                <%--, {field: 'rolename', title: '角色名称', fixed: 'center'}--%>
                                <%--, {field: 'roledesc', title: '角色描述', fixed: 'center'}--%>
                            <%--]]--%>
                        <%--})--%>
                    <%--}--%>
                    <%--, yes: function (obj) {--%>
                        <%--var checkStatus = table.checkStatus("roleTable");--%>
                        <%--if (checkStatus.data.length == 0) {--%>
                            <%--layer.msg("至少选择一个")--%>
                        <%--} else {--%>
                            <%--// 获取用户id--%>
                            <%--var userid = data.userid--%>
                            <%--var param = "userid=" + userid--%>

                            <%--// 循环选中的内容nodes--%>
                            <%--$.each(checkStatus.data, function (i, item) {--%>
                                <%--param += "&ids=" + item.roleid--%>
                            <%--})--%>
                            <%--// A=valueA&B=valueB&B=valueB--%>
                            <%--// 把数据保存到数据库--%>
                            <%--$.get("${pageContext.request.contextPath}/user/saveRoleUser?"+param, function (r) {--%>
                                <%--layer.msg(r.msg)--%>
                            <%--})--%>
                        <%--}--%>
                        <%--layer.close(index)--%>
                    <%--}--%>
                <%--})--%>
            <%--}--%>


        })

</script>
</body>
</html>

